<template>
	<view class="content fafafa mainpadding">
		<view class="flexcenter" v-if="!list.data.length">
			<view class="placeholderimage" style="margin-top: 100rpx;" ></view>
		</view>
		<view class="ffffff mainpadding margin_bottom3 border_2" v-for="item in list.data" :key="item.id" @click="checkAddress(item)">
			<view class="color_ling text_w flexleft">
				<view class="margin_right2">{{item.name}}</view>
				<view>{{item.mobile}}</view>
			</view>
			<view class="margin_top flexleft padding_bottom" style="border-bottom: 1rpx solid rgba(153, 153, 153, .1);">
				<view>
					<image src="/static/images/mine/address.png" mode="" class="addressImg margin_right2"></image>
				</view>
				<view class="text_five color_san">{{item.address}}</view>
			</view>
			<view class="margin_top flexbetween">
				<view class="flexleft" @click.stop="setdefault(item)">
					<view>
						<image src="/static/images/index/xuanzhong.png" v-if="item.is_on==1" mode=""
							class="defaultImg margin_right2"></image>
						<image src="/static/images/index/wiexuanzhong.png" mode="" v-if="item.is_on==0"
							class="defaultImg margin_right2"></image>
					</view>
					<view class="color_san text_b" v-if="item.is_on==1">默認地址</view>
					<view class="color_san text_b" v-if="item.is_on==0">设爲默認</view>
				</view>
				<view class="flexleft">
					<view class="flexleft margin_right6" @click.stop="goAddAddress(item.id)">
						<u-icon name="edit-pen" class="margin_right1" color="#777" size="18"></u-icon>
						<view class="text_b color_san">編輯</view>
					</view>
					<view class="flexleft" @click.stop="deleteAddress(item.id)">
						<u-icon name="trash" class="margin_right1" color="#777" size="18"></u-icon>
						<view class="text_b color_san">刪除</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 底部 -->
		<view class="footer">
			<view class="footerButton" @click="goAddAddress('')">
				+新增收貨地址
			</view>
		</view>
	</view>
</template>

<script>
	// const this.http = require("@/common/this.http.js")
	export default {
		data() {
			return {
				page: 1,
				limit: 12,
				list: {
					data: []
				},
				type: 0
			}
		},
		onLoad(options) {
			if(options.type){
				this.type = options.type
			}
		},
		onShow() {
			this.init();
		},
		methods: {
			init() {
				let _this = this;
				this.http.request('/api/member/listAddre', 'POST', {
					// page: page || 1,
					// limit: _this.limit,
				}, false, false, true).then(res => {
					if (res.code == 1) {
						// let resList = res.data;
						// let dataList = _this.list;
						// if (isPage == true) {
						// 	_this.list.data = dataList.data.concat(resList.data);
						// } else {
						// 	this.list = res.data
						// 	_this.list.data = resList.data;
						// }
						_this.list.data=res.data
					}
				}).catch(err => {
					this.http.toast(err.msg);
				})
			},
			// onReachBottom() {
			// 	let _this = this;
			// 	let list = _this.list;
			// 	let page = _this.page;
			// 	if (list.last_page <= page) {
			// 		return false;
			// 	}
			// 	page++;
			// 	_this.page = page;
			// 	_this.init(true, page);
			// },
			deleteAddress(id) {
				this.http.modal('刪除地址', '您確認要刪除地址吗？', true, (res) => {
					if (res) {
						this.http.request('/api/member/delAddre', 'POST', {
							id
						}).then(resp => {
							if (resp.code == 1) {
								this.http.toast("刪除成功！")
								this.init()
							}else{
								this.http.toast(resp.msg)
							}
						})
					}
				})
			},
			//设爲默認
			setdefault(item) {
				if (!item.is_default) {
					this.http.request('/api/member/isonAddre', 'POST', {
						id: item.id
					}).then(res => {
						if (res.code == 1) {
							this.init()
						}else{
							this.http.toast(res.msg)
						}
					})
				}
			},
			goAddAddress(id) {
				uni.navigateTo({
					url: "/pages_mine/addAddress?id=" + id || ""
				})
			},
			checkAddress(item){
				if(this.type==1){
					uni.$emit("selectAddressFn",item)
					uni.navigateBack(1)
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding-bottom: 130rpx;
	}

	.addressImg {
		width: 26rpx;
		height: 31rpx;
	}

	.defaultImg {
		width: 24rpx;
		height: 24rpx;
		display: block;
	}

	.editImage {
		width: 24rpx;
		height: 28rpx;
	}

	.deleteImage {
		width: 28rpx;
		height: 28rpx;
	}
</style>